home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows (5th Edition) / Programming Windows, 5th ed. - Companion CD (097-0002183)(1999).iso / Chap05 / Clover / Clover.c next >
Encoding:
C/C++ Source or Header  |  1998-10-09  |  4.3 KB  |  126 lines

  1. /*--------------------------------------------------
  2.    CLOVER.C -- Clover Drawing Program Using Regions
  3.                (c) Charles Petzold, 1998
  4.   --------------------------------------------------*/
  5.  
  6. #include <windows.h>
  7. #include <math.h>
  8.  
  9. #define TWO_PI (2.0 * 3.14159)
  10.  
  11. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  12.  
  13. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  14.                     PSTR szCmdLine, int iCmdShow)
  15. {
  16.      static TCHAR szAppName[] = TEXT ("Clover") ;
  17.      HWND         hwnd ;
  18.      MSG          msg ;
  19.      WNDCLASS     wndclass ;
  20.      
  21.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  22.      wndclass.lpfnWndProc   = WndProc ;
  23.      wndclass.cbClsExtra    = 0 ;
  24.      wndclass.cbWndExtra    = 0 ;
  25.      wndclass.hInstance     = hInstance ;
  26.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  27.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  28.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  29.      wndclass.lpszMenuName  = NULL ;
  30.      wndclass.lpszClassName = szAppName ;
  31.      
  32.      if (!RegisterClass (&wndclass))
  33.      {
  34.           MessageBox (NULL, TEXT ("This program requires Windows NT!"),
  35.                       szAppName, MB_ICONERROR) ;
  36.           return 0 ;
  37.      }
  38.      
  39.      hwnd = CreateWindow (szAppName, TEXT ("Draw a Clover"),
  40.                           WS_OVERLAPPEDWINDOW,
  41.                           CW_USEDEFAULT, CW_USEDEFAULT,
  42.                           CW_USEDEFAULT, CW_USEDEFAULT,
  43.                           NULL, NULL, hInstance, NULL) ;
  44.      
  45.      ShowWindow (hwnd, iCmdShow) ;
  46.      UpdateWindow (hwnd) ;
  47.      
  48.      while (GetMessage (&msg, NULL, 0, 0))
  49.      {
  50.           TranslateMessage (&msg) ;
  51.           DispatchMessage (&msg) ;
  52.      }
  53.      return msg.wParam ;
  54. }
  55.  
  56. LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
  57. {
  58.      static HRGN hRgnClip ;
  59.      static int  cxClient, cyClient ;
  60.      double      fAngle, fRadius ;
  61.      HCURSOR     hCursor ;
  62.      HDC         hdc ;
  63.      HRGN        hRgnTemp[6] ;
  64.      int         i ;
  65.      PAINTSTRUCT ps ;
  66.      
  67.      switch (iMsg)
  68.      {
  69.      case WM_SIZE:
  70.           cxClient = LOWORD (lParam) ;
  71.           cyClient = HIWORD (lParam) ;
  72.           
  73.           hCursor = SetCursor (LoadCursor (NULL, IDC_WAIT)) ;
  74.           ShowCursor (TRUE) ;
  75.           
  76.           if (hRgnClip)
  77.                DeleteObject (hRgnClip) ;
  78.           
  79.           hRgnTemp[0] = CreateEllipticRgn (0, cyClient / 3,
  80.                                            cxClient / 2, 2 * cyClient / 3) ;
  81.           hRgnTemp[1] = CreateEllipticRgn (cxClient / 2, cyClient / 3,
  82.                                            cxClient, 2 * cyClient / 3) ;
  83.           hRgnTemp[2] = CreateEllipticRgn (cxClient / 3, 0,
  84.                                            2 * cxClient / 3, cyClient / 2) ;
  85.           hRgnTemp[3] = CreateEllipticRgn (cxClient / 3, cyClient / 2,
  86.                                            2 * cxClient / 3, cyClient) ;
  87.           hRgnTemp[4] = CreateRectRgn (0, 0, 1, 1) ;
  88.           hRgnTemp[5] = CreateRectRgn (0, 0, 1, 1) ;
  89.           hRgnClip    = CreateRectRgn (0, 0, 1, 1) ;
  90.           
  91.           CombineRgn (hRgnTemp[4], hRgnTemp[0], hRgnTemp[1], RGN_OR) ;
  92.           CombineRgn (hRgnTemp[5], hRgnTemp[2], hRgnTemp[3], RGN_OR) ;
  93.           CombineRgn (hRgnClip,    hRgnTemp[4], hRgnTemp[5], RGN_XOR) ;
  94.           
  95.           for (i = 0 ; i < 6 ; i++)
  96.                DeleteObject (hRgnTemp[i]) ;
  97.           
  98.           SetCursor (hCursor) ;
  99.           ShowCursor (FALSE) ;
  100.           return 0 ;
  101.           
  102.      case WM_PAINT:
  103.           hdc = BeginPaint (hwnd, &ps) ;
  104.           
  105.           SetViewportOrgEx (hdc, cxClient / 2, cyClient / 2, NULL) ;
  106.           SelectClipRgn (hdc, hRgnClip) ;
  107.           
  108.           fRadius = _hypot (cxClient / 2.0, cyClient / 2.0) ;
  109.           
  110.           for (fAngle = 0.0 ; fAngle < TWO_PI ; fAngle += TWO_PI / 360)
  111.           {
  112.                MoveToEx (hdc, 0, 0, NULL) ;
  113.                LineTo (hdc, (int) ( fRadius * cos (fAngle) + 0.5),
  114.                             (int) (-fRadius * sin (fAngle) + 0.5)) ;
  115.           }
  116.           EndPaint (hwnd, &ps) ;
  117.           return 0 ;
  118.           
  119.      case WM_DESTROY:
  120.           DeleteObject (hRgnClip) ;
  121.           PostQuitMessage (0) ;
  122.           return 0 ;
  123.      }
  124.      return DefWindowProc (hwnd, iMsg, wParam, lParam) ;
  125. }
  126.